﻿using A.M.ETO;
using AutoMapper;
using DotNetCore.CAP;
using J.M.Inventory.Application.IInventoryServices;
using J.M.Inventory.Domain.DTO;
using J.M.Inventory.Domain.Entity;
using J.M.Inventory.Domain.枚举;
using J.M.Inventory.Infrastruecure.IInventoryRepository;
using J.M.Inventory.Infrastruecure.InventoryRepository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using O.M.Production.Domain.DTO;
using Public_Class_Library;
using Redis_ClientInfor;
using SqlSugar;
using System.Collections.Generic;
using System.Drawing;
using System.Linq.Expressions;
using static O.M.Production.Domain.EnumModel;

namespace J.M.Inventory.Read.API.Controllers
{
    /// <summary>
    /// 入库管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]

    public class Stock_managementReadController : ControllerBase
    {
        private readonly Cross_service_Helper cross_Service_Helper;
        private readonly IStock_managementServices stock_ManagementServices;
        private readonly ICapPublisher capPublisher;
        public Stock_managementReadController(IStock_managementServices stock_ManagementServices, Cross_service_Helper cross_Service_Helper, ICapPublisher capPublisher)
        {
            this.stock_ManagementServices = stock_ManagementServices;
            this.cross_Service_Helper = cross_Service_Helper;
            this.capPublisher = capPublisher;
        }
        /// <summary>
        /// 入库的显示
        /// </summary>
        /// <param name="code">单号查询</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStockShow(string? code, int pageIndex, int pageSize)
        {
            return Ok(stock_ManagementServices.GetStockShow(code, pageIndex, pageSize));
        }
        /// <summary>
        /// 删除入库表
        /// </summary>
        /// <param name="stockid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteStock(string? stockid)
        {
            var res = stock_ManagementServices.DeleteStock(stockid);
            return Ok(res);
        }
        /// <summary>
        /// 查询仓库表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiReturn<warehouse>> GetShowQueryable()
        {
            return await stock_ManagementServices.GetShowQueryable();
        }
        /// <summary>
        /// 新增入库
        /// </summary>
        /// <param name="stock_ManagementAddDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<ApiReturn<string>> GetStockAddInfo(Stock_managementAddDTO stock_ManagementAddDTO)
        {
            return await stock_ManagementServices.GetStockAddInfo(stock_ManagementAddDTO);
        }
        /// <summary>
        /// 调用生产工单
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int pageSize, int pageIndex, string? Work_order_Id)
        {
            ApiResult apiResult = new ApiResult();
            var res = await cross_Service_Helper.Get($"http://10.31.59.2:8088/api/Read_Produce_Work_Order/Get_produce_work_order?Work_order_Id={Work_order_Id}&pageSize={pageSize}&pageIndex={pageIndex}");
            var w = JsonConvert.DeserializeObject<ApiResult>(res);
            return Ok(w);
        }
        /// <summary>
        /// 枚举转换
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TextValue()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<Enter_warehouseEnum>())
            {
                list.Add(new
                {
                    value = item,
                    label = item.ToString()
                });
            }
            return Ok(list);
        }
        /// <summary>
        /// 通过id反填上所选中的生产类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetFindProduct(Enter_warehouseEnum id)
        {
              
            List<ProductDTO> list = new List<ProductDTO>();
            foreach (var item in Enum.GetValues<Enter_warehouseEnum>())
            {
                if(id == item)
                {
                    list.Add(new ProductDTO
                    {
                        value = item,
                        label = item.ToString()
                    });
                }
            }
            return Ok(list.FirstOrDefault());

        }
        /// <summary>
        /// 通过生产单号获取列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        //[HttpGet]
        //public async Task<IActionResult> GetProductShow(string? process_flow_id_name, int pageIndex,int pageSize)
        //{
        //    var res = cross_Service_Helper.Get("http://10.31.59.2:5131/api/Read_Process_Flow/Get_process_flow_Page?process_flow_id_name=2&pageSize=2&pageIndex=1").Result;
        //    var obj = JsonConvert.DeserializeObject<List<internal_product_DTO>>(res);

        //    Expression<Func<internal_product_DTO, bool>> expression = i => true;
        //    if (!string.IsNullOrEmpty(process_flow_id_name))
        //    {
        //        expression = i => i.internal_product_id.Contains(process_flow_id_name) || i.produce_theme.Contains(process_flow_id_name);
        //    }

        //    var list = from a in obj
        //               select new internal_product_DTO
        //               {
        //                   internal_product_id = a.internal_product_id,
        //                   produce_theme = a.produce_theme,
        //                   bill_date = a.bill_date,
        //                   produce_type = a.produce_type,

        //               };
        //    PageETO<internal_product_DTO> pageETO = new PageETO<internal_product_DTO>();
        //    pageETO.TotalCount = list.Count();
        //    pageETO.PageCount = (int)(list.Count() * 1.0 / pageSize);
        //    pageETO.Data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        //    return Ok(pageETO);
        //}


        /// <summary>
        /// 获取Josn字符串
        /// </summary>
        /// <returns></returns>
        [HttpPost]

        public IActionResult GetWlok(GZclassDto gZclassDto)
        {

            return Ok(gZclassDto);
        }


        public class GZclassDto
        {
            public Node[] nodes { get; set; }
            public Edge[] edges { get; set; }
        }

        public class Node
        {
            public string id { get; set; }
            public string type { get; set; }
            public int x { get; set; }
            public int y { get; set; }
            public Properties properties { get; set; }
            public Text text { get; set; }
        }

        public class Properties
        {
            public string? start { get; set; }
            public string? CustomRect { get; set; }
        }

        public class Text
        {
            public int x { get; set; }
            public int y { get; set; }
            public string value { get; set; }
        }

        public class Edge
        {
            public string id { get; set; }
            public string type { get; set; }
            public string sourceNodeId { get; set; }
            public string targetNodeId { get; set; }
            public Startpoint startPoint { get; set; }
            public Endpoint endPoint { get; set; }
            public Properties properties { get; set; }
            public Pointslist[] pointsList { get; set; }
        }

        public class Startpoint
        {
            public int x { get; set; }
            public int y { get; set; }
        }

        public class Endpoint
        {
            public int x { get; set; }
            public int y { get; set; }
        }



        public class Pointslist
        {
            public int x { get; set; }
            public int y { get; set; }
        }
        /// <summary>
        /// 修改入库信息
        /// </summary>
        /// <param name="Stock_managementAddDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiReturn<string>> PostStockUpdateInfo(Stock_managementAddDTO Stock_managementAddDTO)
        {

            return await stock_ManagementServices.PostStockUpdateInfo(Stock_managementAddDTO);
        }
        /// <summary>
        /// 反填入库信息
        /// </summary>
        /// <param name="entermange_id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<stock_management> FindStockInfo(string? entermange_id)
        {
            return await stock_ManagementServices.FindStockInfo(entermange_id);
        }
        /// <summary>
        /// 文件上传到七牛云
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
       public IActionResult Upload(IFormFile file)
        {
             var res =FileHelper.UploadFile(file);
            return Ok (file.FileName);
        }
        /// <summary>
        /// 获取系统入库编号
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetSysCode(string? Url)
        {
            var res = await cross_Service_Helper.Get(Url);
            return Ok(res);
        }
        /// <summary>
        /// 获取部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> TextClient()
        {
            ApiReturn<DepMinrInforDTO> apiReturn = new ApiReturn<DepMinrInforDTO>();
            var res = await cross_Service_Helper.Get($"http://10.31.59.5:8001/api/Organization_chartRead/GetOrgainInforShow?oId=ZJ00000000001");
            var w =JsonConvert.DeserializeObject<ApiReturn<DepMinrInforDTO>>(res);
            return Ok(w);
        }
        /// <summary>
        /// 跨服务调用接口入库编号
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetStackCode(string? Url)
        {
            var res = await cross_Service_Helper.Get(Url);
            return Ok(res);
        }
        /// <summary>
        /// 普通添加
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [CapSubscribe("GetKcName")]
        public async Task<IActionResult> GetAddXsInfor(string? id)
        {
            var res = await stock_ManagementServices.GetAddXsInfor(id);
            return Ok(res);
        }
        /// <summary>
        /// 审批的消息队列
        /// </summary>
        /// <param name="addStaffInforDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInforMQ(Domain.DTO.AprInforGerDTO addStaffInforDTO)
        {
            //申请成功需要传一个消息队列;
            capPublisher.Publish("addStaffInforDTOGer", addStaffInforDTO);
            return Ok("");
        }
        /// <summary>
        /// 先获取所有的审批
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProccInfir()
        {
            var res = "http://10.31.59.5:5123/api/Approval_form/GetappList";
            var mes = await cross_Service_Helper.Get(res);
            var ges = JsonConvert.DeserializeObject<List<Domain.DTO.AddStaffInforDTO>>(mes);
            var list = ges.Where(c => c.approval_name == "库存系统").FirstOrDefault();
            return Ok(list.apprpvalTwoDTO);
        }
    }
}
